package com.hd.boot0407.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@WebFilter("/*")
public class CaculateTimeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        long start = System.currentTimeMillis();
        //让请求继续调用后面的filter和servlet
        chain.doFilter(req, resp);
        long end= System.currentTimeMillis();
        //当前请求走完后续所有的filter和servlet.之后，代码会来到这里
        log.debug("请求{}语执行的时长={}ms" , ((HttpServletRequest)req).getRequestURL() , (end-start));


    }

    @Override
    public void destroy() {

    }

}
